<?php

include("_vars.php");

function write_rules(){
	echo "
<p align='left'>The cricket rules displayed on the home page here are for the traditional form of cricket which is
called 'Test Cricket'. However there are other formats of the game eg. 50 over matches, Twenty20 Cricket etc where
the rules differ slightly.</p>

<p align='center'><strong>Player/Official Cricket Rules</strong></p>

<p align='left'>Cricket is a game played between two teams made up of eleven players each. There is also a reserve
player called a 'twelfth man' who is used should a player be injured during play. The twelfth man is not allowed to
bowl, bat, wicket keep or captain the team. His sole duty is to act as a substiture fielder. The original player is
free to return to the game as soon as they have recovered from their injury.</p>

<p align='left'>To apply the law and make sure the cricket rules are upheld throughout the game there are two
umpires in place during games. Umpires are responsible for making decisions and notifying the scorers of these
decisions. Two umpires are in place on the playing field while there is also a third umpire off the field who is in
charge of video decisions. This is where the call is too close for the on field umpires and they refer it to the
third umpire who reviews slow motion video replays to make a decision.</p>

<p align='center'><strong>Game Structure</strong></p>

<p align='left'>Test cricket is a game that spans over two innings. This means that one team needs to bowl the
other team out twice and score more runs then them to win the match. Another key difference between test cricket
and other forms of cricket is the length of the innings. In test cricket there is no limit to the innings length.
Whereas in one day cricket &amp; Twenty20 cricket there are a certain amount of overs per innings. The only limits
in test cricket is a 5 day length. Before the game begins an official will toss a coin. The captain who guesses the
correct side of the coin will then choose if they want to bat or field first. One team will then bat while the
other will bowl &amp; field. The aim of the batting team is to score runs while the aim of the fielding team is to
bowl ten people out and close the batting teams' innings. Although there are eleven people in each team only ten
people need to be bowled out as you cannot have one person batting alone. Batting is done in pairs.</p>";
}

function write_contact(){
	echo "contact";
}

function write_faq(){
	echo 'faq';
}

function write_results(){
	echo 'results';
}
	
function write_rating(){
	if(!$_SESSION["user"]) echo "you have to be logged in to access this page";
		else  include("add-rating/rating.php"); 
}

function write_home(){
	echo 'home';
}
	
function check_user($user){	
	$user=mysql_real_escape_string($user);
	$mysql="SELECT * FROM  `users` WHERE `email` LIKE \"$user\" LIMIT 1";
	$result=mysql_query($mysql);
	if(mysql_error())echo mysql_error();
	$row = mysql_fetch_array($result);
	
	if($row["email"])return 1;
	return 0;
}
	
function check_credits($user){
	$out="";
	$user=mysql_real_escape_string($user);
	$mysql="SELECT * FROM  `users` WHERE `email` LIKE \"$user\" LIMIT 1";
	$result=mysql_query($mysql);
	if(mysql_error())echo mysql_error();
	$row = mysql_fetch_array($result);
	
	$out="You have accumulated: <b>".$row["credits"]."</b> credits so far.<br />
		 Keep up the good work!<br /><br />
		 In order to  see how to get more credits, check out our rules section.";
	return $out;
}

function check_details($user){
	$out="";
	$user=mysql_real_escape_string($user);
	$mysql="SELECT * FROM  `users` WHERE `email` LIKE \"$user\" LIMIT 1";
	$result=mysql_query($mysql);
	if(mysql_error())echo mysql_error();
	$row = mysql_fetch_array($result);
	
	$out.="You are registered as: ".$row["email"]."<br />";
	$out.="Postcode: ".$row["area"]."<br />";
	$out.="Phone: ".$row["phone"]."<br />";
	
	return $out;
	}

function check_invites($user){
	$web_address= (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
	$web_address=str_replace("side.php","",$web_address);
	$out="";
	$user=mysql_real_escape_string($user);
	$mysql="SELECT * FROM  `users` WHERE `email` LIKE \"$user\" LIMIT 1";
	$result=mysql_query($mysql);
	if(mysql_error())echo mysql_error();
	$row = mysql_fetch_array($result);
	
	$out="Send this link to your friends: <b>".$web_address."?ref=".$row["personal_code"]."</b>";
	return $out;
}

function check_registration($user){
	$user=mysql_real_escape_string($user);
	$mysql="SELECT * FROM  `users` WHERE `email` LIKE \"$user\" LIMIT 1";
	$result=mysql_query($mysql);
	if(mysql_error() || mysql_fetch_array($result)){
		echo mysql_error();
		return 1;
	}
	
	return 0;
}

function register_user($data){
	global $reg_user, $reg_code, $reg_age;
	if(check_registration($data["reg_user"])) return '<b>Email already registered.</b><br /><form><input type="submit" value="Restart procedure" /></form>';
	$complete=1;
	$form=' <hr />
		<form  method="post" id="reg_form">
			<input type="hidden" name="action" value="register" />';

	$out="";
	//$out = $data["reg_user"].$data["reg_code"].$data["reg_age"].$data["reg_phone"];
	if(!$data["reg_user"] || $data["reg_user"]== $reg_user){
		$out.="Email missing<br />";
		$form.='E-mail: <input id="reg_user" type="text" style="position: absolute; right: 10px;" name="reg_user" /><br />';
		$complete=0;
		//form field to be added here rest will be modified accordingly using values provided in initial step.
	}
		else{
			$form.='<input id="reg_user" type="hidden" style="position: absolute; right: 10px;" name="reg_user" value="'.$data["reg_user"].'" />';
		}
	if(!$data["reg_code"] || $data["reg_code"]== $reg_code){
		$out.="Postcode missing<br />";
		$form.='Postcode: <input id="reg_code" type="text" style="position: absolute; right: 10px;" name="reg_code" /><br />';
		$complete=0;
		//form field to be added here rest will be modified accordingly using values provided in initial step.
	}
		else{
			$form.='<input id="reg_code" type="hidden" style="position: absolute; right: 10px;" name="reg_code" value="'.$data["reg_code"].'" />';
		}
	if(!$data["reg_age"]){
		$out.="Age missing<br />";
		$form.='Age: <input id="reg_age" type="text" style="position: absolute; right: 10px;" name="reg_age" /><br />';
		$complete=0;
		//form field to be added here rest will be modified accordingly using values provided in initial step.
	}
		else{
			$form.='<input id="reg_age" type="hidden" style="position: absolute; right: 10px;" name="reg_age" value="'.$data["reg_age"].'" />';
		}
	$form.='<input id="reg_phone" type="hidden" style="position: absolute; right: 10px;" name="reg_phone" value="'.$data["reg_phone"].'" />';
	$form.='	<input type="submit" value="Register" style="position: absolute; right: 10px;" /><br />
		</form><br />';
	
	if($complete){
		//return "REGISTRATION WAS OK";
		$sql = "INSERT INTO `users` (`id`, `email`, `area`, `age`, `phone`, `credits`, `personal_code`, `last_vote`)
				VALUES (NULL, '".$data["reg_user"]."', '".$data["reg_code"]."', '".$data["reg_age"]."', '".$data["reg_phone"]."', '0', '".substr(sha1($data["reg_user"]),0,16)."', NULL);";
		mysql_query($sql);
		$out.='<b>Registration successful!</b><br /><form><input type="submit" value="Proceed to login" /></form>';
		if($_GET["ref"]){
			$sql="SELECT * FROM `users` WHERE `personal_code` LIKE '".$_GET["ref"]."' LIMIT 1";
			$result=mysql_query($sql);
			$rez=mysql_fetch_array($result);
			
			$date = date('Y-m-d');
			if($date!=$rez["last_invite"]){
				$sql="UPDATE `users` SET `last_invite` = '".$date."', `invites_left` = '3' WHERE `id` = ".$rez["id"]." LIMIT 1";
				mysql_query($sql);
			}
			
			$sql="SELECT * FROM `users` WHERE `personal_code` LIKE '".$_GET["ref"]."' LIMIT 1";
			$result=mysql_query($sql);
			$rez=mysql_fetch_array($result);
			
			$out.="Invited by ".$rez["email"]."<br />".$rez["email"]." has received one credit point if he had any invites left.";
			if($rez["invites_left"]){
				$sql="UPDATE `users` SET `invites_left` = '".($rez["invites_left"]-1)."', `credits` =  '".($rez["credits"]+1)."' WHERE  `users`.`email` =  '".$rez["email"]."'";
				mysql_query($sql);
			}
		}
		return $out;
	}

	
	$out='<b>This information is mandatory:</b><br />'.$out;
	$out.='<script type="text/javascript">
			var options = { 
			target: "#slide_frame",   // target element(s) to be updated with server response
			url: "side.php"       // override for forms action attribute 
			}; 
			
		  // $("#reg_form").ajaxForm(options);
		   
		   $("#reg_user").DefaultValue("'.$reg_user.'");
		   $("#reg_code").DefaultValue("'.$reg_code.'");
		   $("#reg_age").DefaultValue("'.$reg_age.'");
		   $("#reg_phone").DefaultValue("'.$reg_phone.'");
		</script>';
	return $out.$form;
}
?> 
